package com.jielin.handling.web.httpservlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**  
 * <p>Title: DownloadProxyServlet.java</p>  
 * <p>Description: 在SpringBoot中通过代码注册的方式来注册Servlet
 * 在本地加一个Servlet接受前台请求，之后再由后台转发，就不涉及跨域的问题啦。
 * </p>  
 * @author WR  
 * @date 2019年5月23日  
 * @version 1.0  
 */ 
public class DownloadProxyServlet extends HttpServlet {  
    int READ_BUFFER_SIZE=10*1024;  
     public void doPost(HttpServletRequest request, HttpServletResponse response)    
             throws ServletException, IOException {    
         String urlString = request.getParameter("url");   
         Enumeration parameterNames = request.getParameterNames();    
         StringBuffer parammeters = new StringBuffer("?");    
         while (parameterNames.hasMoreElements()) {    
             String paramName = (String) parameterNames.nextElement();    
             String paramValue = request.getParameter(paramName);    
             String composeParameter = composeParamenter(paramName, paramValue);    
             parammeters.append(composeParameter);    
             parammeters.append("&");    
         }    
         int paramerterSize = parammeters.length();    
         parammeters.deleteCharAt(paramerterSize - 1);    
         writeResponse(response, urlString, parammeters);    
     }    
   
     private void writeResponse(HttpServletResponse response, String urlString,    
             StringBuffer parameters) throws ServletException {    
         try {    
             URL url = new URL(urlString + parameters.toString());    
             URLConnection urlConnection = url.openConnection();    
             response.setContentType(urlConnection.getContentType());    
             InputStream ins = urlConnection.getInputStream();    
             OutputStream outs = response.getOutputStream();    
             byte[] buffer = new byte[READ_BUFFER_SIZE];    
             int bytesRead = 0;    
             while ((bytesRead = ins.read(buffer, 0, READ_BUFFER_SIZE)) != -1) {    
                 outs.write(buffer, 0, bytesRead);    
             }    
             outs.flush();    
             outs.close();    
             ins.close();    
         } catch (Exception e) {    
             try {    
                 response.sendError(    
                         HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e    
                                 .getMessage());    
             } catch (IOException ioe) {    
                 throw new ServletException(ioe);    
             }    
         }    
     }    
   
     private String composeParamenter(String paramName, String paramValue) {    
         if (paramName != null && !"".equals(paramName.trim())    
                 && paramValue != null && !"".equals(paramValue.trim())) {    
             return (paramName + "=" + paramValue);    
         }    
         return "";    
     }    
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
           doPost(request,response);
}
}
